package com.ef.web.action;

import java.util.List;

import com.ef.constant.ConfirmMessage;
import com.ef.domain.ClassType;
import com.ef.domain.CorpClass;
import com.ef.domain.Teacher;
import com.ef.service.ClassTypeService;
import com.ef.service.CorpClassService;
import com.ef.service.CorpContractService;
import com.ef.service.TeacherService;

public class CorpClassAction extends BaseAction<CorpClass> {

	private static final long serialVersionUID = 1L;

	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(CorpClassAction.class);

	private CorpContractService corpContractService;

	private CorpClassService corpClassService;

	private TeacherService teacherService;

	private ClassTypeService classTypeService;

	private List<ClassType> classTypeList;

	private List<Teacher> teacherList;

	private String selContractId;

	private String selClasstypeId;

	private String selTeacherId;

	public String edit() {
		fetchListData();
		return EDIT;
	}

	public String save() {
		CorpClass corpClass = this.getModel();
		if (corpClass.getStartTime().after(corpClass.getEndTime())){
			this.resultError("start time mast before the end time!", "../CorpContract/redirect?id="+selContractId);
			fetchListData();
			return EDIT;
		}
		if (corpClass.getId() == null) {
			corpClass.setContract(corpContractService.findById(Long
					.valueOf(selContractId.trim())));
		}
		if (selClasstypeId != null && selClasstypeId.trim().length() > 0) {
			corpClass.setClasstype(classTypeService.findById(Long
					.valueOf(selClasstypeId.trim())));
		} else {
			corpClass.setClasstype(null);
		}
		if (selTeacherId != null && selTeacherId.trim().length() > 0) {
			corpClass.setTeacher(teacherService.findById(Long
					.valueOf(selTeacherId.trim())));
		} else {
			corpClass.setTeacher(null);
		}
		corpClassService.saveOrUpdate(corpClass);
		this.resultOK(ConfirmMessage.Contract,"../CorpContract/redirect?id="+selContractId);
		fetchListData();
		return EDIT;
	}
	
	public String delete(){
		CorpClass corpClass = this.getModel();
		corpClassService.delete(corpClass);
		return "contract";
	}

	private void fetchListData() {
		teacherList = teacherService.getAllValidCLTTeachers();
		classTypeList = classTypeService.findALLValid();
		if (this.getModel().getClasstype() != null) {
			selClasstypeId = this.getModel().getClasstype().getId().toString();
		}
		if (this.getModel().getTeacher() != null) {
			selTeacherId = this.getModel().getTeacher().getId().toString();
		}
	}

	public CorpContractService getCorpContractService() {
		return corpContractService;
	}

	public void setCorpContractService(CorpContractService corpContractService) {
		this.corpContractService = corpContractService;
	}

	public CorpClassService getCorpClassService() {
		return corpClassService;
	}

	public void setCorpClassService(CorpClassService corpClassService) {
		this.corpClassService = corpClassService;
	}

	public TeacherService getTeacherService() {
		return teacherService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public ClassTypeService getClassTypeService() {
		return classTypeService;
	}

	public void setClassTypeService(ClassTypeService classTypeService) {
		this.classTypeService = classTypeService;
	}

	public List<ClassType> getClassTypeList() {
		return classTypeList;
	}

	public void setClassTypeList(List<ClassType> classTypeList) {
		this.classTypeList = classTypeList;
	}

	public List<Teacher> getTeacherList() {
		return teacherList;
	}

	public void setTeacherList(List<Teacher> teacherList) {
		this.teacherList = teacherList;
	}

	public String getSelContractId() {
		return selContractId;
	}

	public void setSelContractId(String selContractId) {
		this.selContractId = selContractId;
	}

	public String getSelClasstypeId() {
		return selClasstypeId;
	}

	public void setSelClasstypeId(String selClasstypeId) {
		this.selClasstypeId = selClasstypeId;
	}

	public String getSelTeacherId() {
		return selTeacherId;
	}

	public void setSelTeacherId(String selTeacherId) {
		this.selTeacherId = selTeacherId;
	}
}
